const app = getApp();
const {
  AskPage
} = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
    /**
     * 页面的初始数据
     */
    name: "changePassword",
    mixins: [formMixin],
    data: {
      statusBar: app.globalData.StatusBar,
      customBar: app.globalData.CustomBar,
      defaultPsd: "",
      newPassShow: true,
      confirmPassShow: true,
      send: true,
      alreadySend: false,
      second: 60,
      disabled: true,
      errorPsd: "",
      errorPsdNew: "",
      errorPsdConfirm: "",
      show_pwd:false,
      model: {
        password: "",
        newPsd: "",
        confirmPsd: ""
      }

    },

    onInput(e) {
      const value = e.detail.value;
      console.log(e)
      this.setModel({
        currentTarget: e.currentTarget,
        detail: {
          value,
        },
      });
    },

    checkPsd() {
      let errorPsd = this.data.errorPsd
      if (!this.data.model.password) {
        errorPsd = '请输入密码';
      }
      // else if (this.data.model.password != this.data.defaultPsd) {
      //   errorPsd = '密码错误';
      //   console.log( this.errorPsd);
      // } 
      else {
        errorPsd = '';
      }
      this.setData({
        errorPsd
      })
    },
    checkPsdNew() {

      // let errorPsdNew = this.data.errorPsdNew
      // if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      //   errorPsdNew='两次密码不同'
      // }else{
      //   errorPsdNew=''
      // }
      // this.setData({errorPsdNew})

      // let errorPsdConfirm = this.data.errorPsdConfirm
      // if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      //   errorPsdConfirm='两次密码不同'
      // }else{
      //   errorPsdConfirm=''
      // }
      // this.setData({errorPsdConfirm})

    },
    checkPsdConfirm() {
      // let errorPsdNew = this.data.errorPsdNew
      // if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      //   errorPsdNew='两次密码不同'
      //   console.log('两次密码不同');
      // }else{
      //   errorPsdNew=''
      // }
      // this.setData({errorPsdNew})

      // let errorPsdConfirm = this.data.errorPsdConfirm
      // if((this.data.model.newPsd!=this.data.model.confirmPsd)&&(this.data.model.newPsd!='')&&(this.data.model.confirmPsd!='')){
      //   errorPsdConfirm='两次密码不同'
      //   console.log('两次密码不同');
      // }else{
      //   errorPsdConfirm=''
      // }
      // this.setData({errorPsdConfirm})
    },
  async  save() {
      let {
        password,
        newPsd,
        confirmPsd
      } = this.data.model;

      console.log(password, newPsd, confirmPsd);

      let res = this.validateFiled([
        [password, {
          required: "请输入账号!"
        }],
        [newPsd, {
          required: "请输入验证码!"
        }],
        [confirmPsd, {
          required: "请确认新密码!"
        }],
      ]);
      if (!res.result) {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
        return false;
      }
      // console.log(this.data.model.password);
      //这是一个json对象
      var jsonobj = {
        "tel": password,
        "code": newPsd,
        "password": confirmPsd,
      };
      //json对象转成json字符串
      // var findUser = JSON.stringify(jsonobj)
      const response = await this.$_server("user/findPassword", {
        data:jsonobj
    });
    let { data } = response;
    console.log(data);

    let message  = data.message;
    wx.showToast({
     title: message,
     icon: 'none',
     duration: 2000
   })
    if(data.code!=200){
      return
    }
    this.goBack();

      // request.post(
      //   "/applet/user/findPassword",
      //   jsonobj
      // ).then(ret => {
      //   wx.showToast({
      //     title: '操作成功',
      //     icon: 'none',
      //     duration: 2000
      //   });
      //   this.goBack();
      //   // console.log(ret);
      // });
    },
    onIconClickNew() {
      this.setData({
        newPassShow: !this.data.newPassShow,
      });
    },
    onIconClickConfirm() {
      this.setData({
        confirmPassShow: !this.data.confirmPassShow,
      });
    },
    goBack() {
      wx.navigateBack({
        delta: 0,
      })
    },
    async sendMsg() {
      var phoneNum = this.data.model.password;
      // console.log(phoneNum);

      if (phoneNum == '' || !phoneNum) {
        wx.showToast({
          title: '请输入手机号码',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      this.setData({
        alreadySend: true,
        send: false
      })
      this.timer()

      const response = await this.$_server("user/sendCode", {
        params: { 
          tel: "?tel="+phoneNum
        },
    });
    let { data } = response;
    // if (data.code !== 200 || (data.code === 200 && !data.data.token)) {
        
    //     throw data;
    // }

    let message  = data.message;
    wx.showToast({
     title: message,
     icon: 'none',
     duration: 2000
   })
    console.log(data);

    },
    showSendMsg: function () {
      if (!this.data.alreadySend) {
        this.setData({
          send: true
        })
      }
    },
    hideSendMsg: function () {
      this.setData({
        send: false,
        disabled: true,
        buttonType: 'default'
      })
    },
    timer: function () {
      let promise = new Promise((resolve, reject) => {
        let setTimer = setInterval(
          () => {
            this.setData({
              second: this.data.second - 1
            })
            if (this.data.second <= 0) {
              this.setData({
                second: 60,
                alreadySend: false,
                send: true
              })
              resolve(setTimer)
            }
          }, 1000)
      })
      promise.then((setTimer) => {
        clearInterval(setTimer)
      })
    },
    cancleBoxAction() {
      // console.log("==取消==");
      this.goBack();
    },
    customerBoxAction() {
      this.selectComponent('#bottomFrame').showFrame();
    },
    hideFrame() {
      this.selectComponent('#bottomFrame').hideFrame();
    },
    dialNumber() {
      wx.makePhoneCall({
        phoneNumber: '400-8618-890',
        success: function () {
          console.log('拨打成功')
        },
        fail: function () {
          console.log('拨打失败')
        }
      })
      this.selectComponent('#bottomFrame').hideFrame();
    },
    advisory() {
      wx.setClipboardData({
        data:'天作云平台',
        success: function (res) {
          wx.showToast({
            title: '复制成功',
          });
        }
      });
  
      this.selectComponent('#bottomFrame').hideFrame();
    },
          //显示密码
          clickShow(){
            // console.log(this.data.show_pwd);
            this.setData({
              "show_pwd":!this.data.show_pwd
            })
          }
    
    
    

  }


);